package wangmin.message.mgr_web.web.controller;

import com.beust.jcommander.internal.Lists;
import com.dap.dao.model.Page;
import com.sbp.message.api.entity.MessageStatus;
import com.sbp.message.api.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wangmin.message.mgr_web.web.entity.CommonResponseBody;
import com.sbp.message.api.entity.MessageVO;
import wangmin.message.mgr_web.web.entity.type.CommonResponseCode;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>User: Wang Min
 * <p>Date: 2016-12-20
 * <p>Version: 1.0
 */
@RestController
@RequestMapping("/message")
@Api(description = "数据项的相关操作")
public class MessageController {

    @Autowired
    private MessageService messageService;


	@RequestMapping(value = "/page", method = RequestMethod.GET, produces = "application/json")
    @ApiOperation(value = "消息分页查询")
    public Page<MessageVO> page(
            @RequestParam("messageStatus") MessageStatus messageStatus,
            @RequestParam("orderBy") String orderByFieldName,
            @RequestParam("desc") boolean desc,
            @RequestParam("offset") int offset,
            @RequestParam("limit") int limit) {
        Map<String, Object> parameter = new HashMap<>();
        parameter.put("status", messageStatus.getValue());
        parameter.put("orderByField", orderByFieldName);
        if (desc)
            parameter.put("desc", desc);
        return messageService.queryMessageVOPage(parameter, 1 + offset / limit, limit);
    }

    @RequestMapping(value = "/sendMessage/{messageId}", method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "发送消息")
    public CommonResponseBody sendMessage(@PathVariable("messageId") String messageId) {
        if (null != messageService.sendMessage(messageId))
            return new CommonResponseBody();
        else
            return new CommonResponseBody(CommonResponseCode.paramError, "messageId["+messageId+"], not exist");
    }
    @RequestMapping(value = "/reliveAndSendMessage/{messageId}", method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "复活并发送消息")
    public CommonResponseBody reliveAndSendMessage(@PathVariable("messageId") String messageId) {
        if (messageService.reliveAndSendMessage(messageId))
            return new CommonResponseBody();
        else
            return new CommonResponseBody(CommonResponseCode.paramError, "messageId["+messageId+"], not exist");
    }

    @RequestMapping(value = "/updateMessageContent/{messageId}", method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "修改消息内容")
    public CommonResponseBody updateMessageContent(@PathVariable("messageId") String messageId, String messageContent) {
        if (messageService.updateMessageContent(messageId, messageContent) > 0)
            return new CommonResponseBody();
        else
            return new CommonResponseBody(CommonResponseCode.paramError, "messageId["+messageId+"], update content error");
    }

    @RequestMapping(value = "/removeMessage/{messageId}", method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "删除消息")
    public CommonResponseBody removeMessage(@PathVariable("messageId") String messageId) {
        if (messageService.removeMessages(Lists.newArrayList(messageId)) > 0)
            return new CommonResponseBody();
        else
            return new CommonResponseBody(CommonResponseCode.paramError, "messageId["+messageId+"], remove error");
    }
}
